package org.rooster.chromosomeevaluator.rules;

import org.jgap.Gene;
import org.jgap.IChromosome;
import org.rooster.genetic.GeneIndex;
import org.rooster.genetic.GenericRule;
import org.rooster.manager.ResourceCapabilitiesTracker;
import org.rooster.manager.TaskManager;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: manorama
 * Date: 8/10/12
 * Time: 8:12 PM
 * To change this template use File | Settings | File Templates.
 */
public class EmployeeCapabilityCheckingPenaltyRule implements GenericRule{
    
    private int weight = 1;
    private ArrayList<int[]> incapableAssignments = new ArrayList<int[]>();

    public EmployeeCapabilityCheckingPenaltyRule() {
    }

    public EmployeeCapabilityCheckingPenaltyRule(int weight) {
        this.weight = weight;
    }

    @Override
    public double fitnessValueForRule(IChromosome chromosome) {
        int penalty = 1;

        int sectionStartIndex;
        int sectionEndIndex;
        Gene[] genes = chromosome.getGenes();
        for(int i=0; i< TaskManager.NO_OF_SECTIONS; i++) {
            sectionStartIndex = GeneIndex.getSectionStart(i);
            sectionEndIndex = GeneIndex.getSectionEnd(i);
            for (int j=sectionStartIndex; j<=sectionEndIndex; j++) {
                if(ResourceCapabilitiesTracker.getInstance().getEmployeeCapability((Integer) genes[j].getAllele(), i) == 0){
                    penalty++;
                    incapableAssignments.add(new int[]{(Integer)genes[j].getAllele()-1,i});
                }
            }
        }
        return 1.0/(penalty * weight);
    }

    @Override
    public int getWeight() {
        return weight;
    }

    @Override
    public void setWeight(int value) {
        this.weight = value;
    }

    /**
     * @return the incapableAssignments
     */
    public ArrayList<int[]> getIncapableAssignments() {
        return incapableAssignments;
    }
}
